package krow.dev.contest;


import java.util.*;

import krow.dev.contest.Constants.Invoke;
import krow.dev.contest.controller.*;
import krow.dev.contest.model.*;
import krow.dev.contest.view.*;
import android.*;
import android.app.*;
import android.content.*;
import android.os.*;
import android.util.*;
import android.widget.*;

public class SubActivity extends Activity {

	private RssReader mRssReaderTask;
	private MenuListAdapter mMenuListAdapter;
	private ListView mMenuListView;
	
	private String mTitle;
	private String mRssAddr;
	private TextView tv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Intent intent = getIntent();
		if (intent != null) {
			mTitle = intent.getStringExtra(Invoke.KEY_TITLE);
			mRssAddr = intent.getStringExtra(Invoke.KEY_RssAddr);
			Log.i("서브액티비티", "KEY_RssAddr 값은 - " + mRssAddr);
		}
		
		initializeView();
		
		buildMenu(mRssAddr);
	}
	
	private void initializeView(){
		
		mMenuListAdapter = new MenuListAdapter(getApplicationContext());
		mMenuListView = (ListView) findViewById(R.id.list_menu);
		mMenuListView.addHeaderView(getLayoutInflater().inflate(R.layout.header_menu, null));
		//mMenuListView.setOnItemClickListener(mMenuItemClickListener);
		//tv.findViewById(R.id.header_tv);
		//tv.setText(mTitle);
		mMenuListView.setAdapter(mMenuListAdapter);
	}
	
	@Override
	protected void onStop() {
		if(mRssReaderTask != null){
			mRssReaderTask.cancel();
		}
		super.onStop();
	}
	
	private void buildMenu(String s){
		
		mRssReaderTask = new RssReader(this);
		mRssReaderTask.setCompletionListener(mBuildCompletionListener);
		if(s.equals("department")){
			Log.i("서브액티비티-빌드메뉴함수", "스트링이 디파트멘트일떄 진입함");
			mRssReaderTask.addMenu(
					0, 
					getString(R.string.department_0101), "http://rss.campusmon.com/contest_0101.xml");
			mRssReaderTask.addMenu(
					0, 
					getString(R.string.department_0102), "http://rss.campusmon.com/contest_0102.xml");
			mRssReaderTask.addMenu(
					0,
					getString(R.string.department_0103), "http://rss.campusmon.com/contest_0103.xml");
			mRssReaderTask.addMenu(
					0,
					getString(R.string.department_0105), "http://rss.campusmon.com/contest_0105.xml");
			mRssReaderTask.addMenu(
					0,
					getString(R.string.department_0106), "http://rss.campusmon.com/contest_0106.xml");
			mRssReaderTask.addMenu(
					0,
					getString(R.string.department_0107), "http://rss.campusmon.com/contest_0107.xml");
			mRssReaderTask.addMenu(
					0,
					getString(R.string.department_0109), "http://rss.campusmon.com/contest_0109.xml");
			mRssReaderTask.addMenu(
					0,
					getString(R.string.department_0111), "http://rss.campusmon.com/contest_0111.xml");
			mRssReaderTask.addMenu(
					0,
					getString(R.string.department_0113), "http://rss.campusmon.com/contest_0113.xml");
			mRssReaderTask.addMenu(
					0,
					getString(R.string.department_0114), "http://rss.campusmon.com/contest_0114.xml");
			mRssReaderTask.addMenu(
					0,
					getString(R.string.department_0115), "http://rss.campusmon.com/contest_0115.xml");
			mRssReaderTask.addMenu(
					0,
					getString(R.string.department_0116), "http://rss.campusmon.com/contest_0116.xml");
			mRssReaderTask.addMenu(
					0,
					getString(R.string.department_0120), "http://rss.campusmon.com/contest_0120.xml");
			mRssReaderTask.addMenu(
					0,
					getString(R.string.department_0150), "http://rss.campusmon.com/contest_0150.xml");
			mRssReaderTask.addMenu(
					0,
					getString(R.string.department_0180), "http://rss.campusmon.com/contest_0180.xml");
			
			mRssReaderTask.execute();
			
		}
		
	
		//TODO push, 전화 걸기, 데이터 수집 등 10개 안팍으로 메뉴 구성 예정
		
		
	}
	
	private RssReader.OnCompletionListener mBuildCompletionListener = new RssReader.OnCompletionListener() {
		
		@Override
		public void onCompletion(List<MenuItem> menuItemList) {
			
			mMenuListAdapter.setMenuItemList(menuItemList);
			mMenuListAdapter.notifyDataSetChanged();
		}
	};
}
